unit FileProvider;

{
  This unit provides file paths at different os-systems and different runtime enviroments,
  as well as it takes care of different paths at debug- and run-time.
}


{$mode objfpc}{$H+}

interface

uses
  Forms, Classes, SysUtils, FileUtil;

type
  TFileType = (FT_Data,            //the data directory, stores for example the dice pics
               FT_DebugOutput);    //the for example the memory log is stored there

  { TFileProvider }
  TFileProvider = class
    private
      FBasicDataPath: String;
      FBasicDebugOutputPath: String;
    public
      function ProvideFile(APath: String; AFileType: TFileType): String;
    public
      constructor Create;
      destructor Destroy; override;
  end;


implementation

{ TFileProvider }

function TFileProvider.ProvideFile(APath: String; AFileType: TFileType): String;
begin
  {returns the path to to the file}
  case AFileType of
    FT_Data: result := CreateAbsolutePath(APath,FBasicDataPath);
    FT_DebugOutput: result := CreateAbsolutePath(APath,FBasicDebugOutputPath)
  end;
end;


constructor TFileProvider.Create;
begin
  {gather all basic paths, if they do not exist, create them}
  if Application.HasOption ('data') then
    FBasicDataPath := Application.GetOptionValue('data')
  else
    FBasicDataPath := ExtractFilePath(Application.ExeName)+'data\';
  CreateAbsolutePath(FBasicDataPath, ExtractFilePath(Paramstr(0)));
  if not DirectoryExists(FBasicDataPath) then CreateDir(FBasicDataPath);

  if Application.HasOption ('debug') then
    FBasicDebugOutputPath := Application.GetOptionValue('debug')
  else
    FBasicDebugOutputPath := ExtractFilePath(Application.ExeName)+'debug\';
  CreateAbsolutePath(FBasicDebugOutputPath, ExtractFilePath(Paramstr(0)));
  if not DirectoryExists(FBasicDebugOutputPath) then CreateDir(FBasicDebugOutputPath);

  inherited Create;
end;

destructor TFileProvider.Destroy;
begin
  inherited Destroy;
end;

end.

